package chapter06

/**
 * 继承和多态
 */
object Demo07_Inherit {
  def main(args: Array[String]): Unit = {
    val student1: Student7 = new Student7("alice" , 18)
    student1.printInfo()
    val student2 = new Student7("bob" ,99,"std000")
    student2.printInfo()

    val teacher = new Teacher
    teacher.printInfo()


    //动态绑定 ，在编译期间不直接绑定方法 在运行时再进行绑定方法  这就是多态
    def personInfo(person: Person7): Unit = {
       person.printInfo()
    }

    println("===================================")
    //把子类或者父类的对象示例传给父类或者接口的引用 可以实现同一接口有不同状态 多种不同的实现
    val person = new Person7
    personInfo(student1)
    personInfo(teacher)
    personInfo(person)

  }

}

//定义一个父类
//class Person7(){}表示当前Person类中主构造器是无参的
class Person7(){
  var name:String = _
  var age: Int = _

  println("1、父类的主构造器调用")

  def this(name: String , age: Int){
    this()
    println("2、父类的辅助构造器被调用")
    this.name = name
    this.age  = age
  }

  def printInfo(): Unit = {
    println(s"Person: $name $age")
  }
}
//定义子类
//直接写person7 的话就相当于直接调用Person7的主构造器
class Student7(name: String , age: Int) extends Person7(){
  var stdNo: String = _
  println("3、子类的主构造器被调用")

  def this(name: String , age: Int,stdNo: String){
    this(name , age)
    println("4、子类的辅助构造器调用")
    this.stdNo = stdNo
  }

  override def printInfo(): Unit = {
    println(s"Person: $name $age $stdNo")
  }
}

class Teacher extends Person7{
  override def printInfo(): Unit = {
    println(s"Teacher: teacher")
  }
}